package com.liyunc.demo.bundle.user.org.web.resources;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.liyunc.demo.bundle.user.org.model.dto.input.PersonManagerRegisterRequestDto;
import com.liyunc.demo.bundle.user.org.model.dto.input.PersonRegisterRequestDto;
import com.liyunc.demo.bundle.user.org.model.dto.input.PersonResetPasswordRequestDto;
import com.liyunc.demo.bundle.user.org.model.dto.input.PersonUpdateRequestDto;
import com.liyunc.demo.bundle.user.org.model.dto.output.UserPersonalAccountDto;
import com.liyunc.demo.bundle.user.org.model.dto.output.PersonRegisterResponseDto;
import com.liyunc.demo.bundle.user.org.service.UserPersonalAccountService;
import com.liyunc.demo.comp.common.rest.request.PageRequest;
import com.liyunc.demo.comp.common.rest.response.GeneralResponse;
import com.liyunc.demo.comp.common.rest.response.PageableResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Optional;

/**
 * @author liyuncong
 * @version 1.0
 * @file UserinfoResource.java
 * @brief UserinfoResource
 * @details UserinfoResource
 * @date 2022-04-01
 *
 * Edit History
 * ----------------------------------------------------------------------------
 * DATE                        NAME               DESCRIPTION
 * 2022-04-01                  liyuncong          Created
 */
@Tag(name = "用户信息相关接口")
@RestController
@RequestMapping(
    value = "/api/v1/people",
    produces = MediaType.APPLICATION_JSON_VALUE
)
@RequiredArgsConstructor
@Validated
public class PersonResource {

    @Autowired
    private UserPersonalAccountService accountService;

    @RequestMapping(
        method = RequestMethod.POST,
        path = "",
        consumes = {MediaType.APPLICATION_JSON_VALUE},
        params = "action=register"
    )
    @Operation(description = "用户注册")
    public GeneralResponse<PersonRegisterResponseDto> register(
        @Valid @NotNull @RequestBody PersonRegisterRequestDto requestDto
    ) {
        return GeneralResponse.of(accountService.register(requestDto).orElse(null));
    }

    @RequestMapping(
        method = RequestMethod.POST,
        path = "",
        consumes = {MediaType.APPLICATION_JSON_VALUE},
        params = "action=manager_create"
    )
    @Operation(description = "管理员创建用户")
    public GeneralResponse<PersonRegisterResponseDto> managerCreatePerson(
        @Valid @NotNull @RequestBody PersonManagerRegisterRequestDto requestDto
    ) {
        return GeneralResponse.of(
            accountService.managerCreatePerson(requestDto).orElse(null)
        );
    }

    @RequestMapping(
        method = RequestMethod.GET,
        path = "/me"
    )
    @Operation(description = "获取当前登录用户信息")
    public GeneralResponse<UserPersonalAccountDto> getLoginUserInfo() {
        return GeneralResponse.of(
            accountService.getCurrentLoginPersonInfo().orElse(null)
        );
    }

    @RequestMapping(
        method = RequestMethod.PUT,
        path = "/me",
        params = "action=reset_password"
    )
    @Operation(description = "用户重置密码")
    public GeneralResponse<UserPersonalAccountDto> resetPassword(
        @RequestBody PersonResetPasswordRequestDto requestDto
    ) {
        return GeneralResponse.of(
            accountService.resetPassword(requestDto).orElse(null)
        );
    }

    @RequestMapping(
        method = RequestMethod.PUT,
        path = "/me",
        params = "action=update"
    )
    @Operation(description = "用户修改个人信息")
    public GeneralResponse<UserPersonalAccountDto> updatePersonInfo(
        @NotNull @RequestBody PersonUpdateRequestDto requestDto
    ) {
        return GeneralResponse.of(
            accountService.updatePersonInfo(requestDto).orElse(null)
        );
    }

    @RequestMapping(
        method = RequestMethod.PUT,
        path = "/{userPersonalAccountId}",
        consumes = MediaType.APPLICATION_JSON_VALUE
    )
    @Operation(description = "用户账号停用、启用")
    public GeneralResponse<UserPersonalAccountDto> updateAccountStatus(
        @NotNull @PathVariable("userPersonalAccountId") Long userPersonalAccountId,
        @NotNull @RequestParam("action") String type
    ) {
        final Integer status = type.equalsIgnoreCase("disable") ? 0 : 1;
        return GeneralResponse.of(
            accountService.updateAccountStatus(userPersonalAccountId, status).orElse(null)
        );
    }

    @RequestMapping(
        method = RequestMethod.GET,
        path = "",
        params = "action=search"
    )
    @Operation(description = "查询用户账号列表")
    public PageableResponse<UserPersonalAccountDto> searchUserPersonalAccounts(
        @RequestParam(name = "userOrganizationAccountId", required = false) Long accountId,
        @RequestParam(name = "keyword", required = false) String keyword,
        @Valid @NotNull PageRequest pageRequest
    ) {
        Optional<Pair<IPage<?>, List<UserPersonalAccountDto>>> result =
            accountService.searchUserPersonalAccounts(accountId, pageRequest, keyword);
        return result.map(item ->
                PageableResponse.of(item.getRight(), item.getLeft()))
            .orElseGet(PageableResponse::empty);
    }
}
